/*
 * Copyright (c) 2019- 2019 threefish(https://gitee.com/threefish https://github.com/threefish) All Rights Reserved.
 * 本项目完全开源，商用完全免费。但请勿侵犯作者合法权益，如申请软著等。
 * 最后修改时间：2019/10/07 18:26:07
 * 源 码 地 址：https://gitee.com/threefish/NutzFw
 */

package com.glp.work.flow.workflow.plugin.flowable.util;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.flowable.bpmn.model.BaseElement;
import org.flowable.bpmn.model.ExtensionElement;
import org.flowable.bpmn.model.UserTask;
import org.flowable.editor.constants.ModelDataJsonConstants;
import org.flowable.editor.language.json.converter.BpmnJsonConverter;
import org.flowable.engine.impl.bpmn.behavior.MultiInstanceActivityBehavior;
import org.flowable.engine.repository.Model;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskInfo;
import org.flowable.task.api.TaskQuery;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.glp.work.flow.constant.TaskStatusEnum;
import com.glp.work.flow.pojo.FlowTaskDto;
import com.glp.work.flow.workflow.plugin.flowable.converter.CustomUserTaskJsonConverter;
import com.glp.work.flow.workflow.plugin.flowable.dto.UserTaskExtensionDTO;
import com.google.common.collect.Maps;
import com.google.gson.Gson;

import springfox.documentation.spring.web.json.Json;

/**
 * @author Pan Wang
 * @date: 2019/4/9
 */
public class FlowUtils {

    public static ExtensionElement buildExtensionElement(String name, String textValue) {
        ExtensionElement extensionElement = new ExtensionElement();
        extensionElement.setName(name);
        extensionElement.setNamespacePrefix("GlpWorkFlow");
        extensionElement.setNamespace(BpmnJsonConverter.MODELER_NAMESPACE);
        extensionElement.setElementText(textValue);
        return extensionElement;
    }
    
    public static void buildTodoQuery(TaskQuery todoTaskQuery, String userName, List<String> roleCodes) {
        todoTaskQuery.or().taskAssignee(userName).taskCandidateUser(userName);
        if (CollectionUtils.isNotEmpty(roleCodes)) {
            todoTaskQuery.taskCandidateGroupIn(roleCodes);
        }
        todoTaskQuery.endOr();
    }

    public static UserTaskExtensionDTO getUserTaskExtension(UserTask userTask) {
        UserTaskExtensionDTO dto = null;
        if (userTask != null) {
            List<ExtensionElement> properites = userTask.getExtensionElements().get(CustomUserTaskJsonConverter.USER_TASK_EXTENSION_ELEMENT_NAME);
            if (properites.size() > 0) {
                String extensionElementText = properites.get(0).getElementText();
                if (StringUtils.isNotBlank(extensionElementText)) {
                    dto = new Gson().fromJson(extensionElementText, UserTaskExtensionDTO.class);
                }
            }
            if (userTask.getBehavior() instanceof MultiInstanceActivityBehavior && dto != null) {
                //是多实例节点
                dto.setMultiInstanceNode(true);
            }
        }
        if (dto != null) {
            dto.setUserTaskFormKey(userTask.getFormKey());
            dto.setUserTaskName(userTask.getName());
            dto.setUserTaskId(userTask.getId());
            dto.setUserTaskDocumentation(userTask.getDocumentation());
        }
        return dto;
    }
    
    public static Model buildModel(Model model, String categoryId, String name, String key, String description,Integer version) {
        description = StringUtils.trimToEmpty(description);
        name = StringUtils.trimToEmpty(name);
        key = StringUtils.trimToEmpty(key).replaceAll(" ", "");
        /*Map<String,Object> nutMap = Maps.newHashMap();
        nutMap.put("category", categoryId);
        nutMap.put("key", key);
        nutMap.put(ModelDataJsonConstants.MODEL_NAME, name);
        nutMap.put(ModelDataJsonConstants.MODEL_REVISION, version);
        nutMap.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
        model.setMetaInfo(new Gson().toJson(nutMap));*/
        model.setMetaInfo(description);
        model.setKey(key);
        model.setName(name);
        model.setCategory(categoryId);
        model.setKey(key);
        model.setVersion(version);
        return model;
    }    
}
